package app.netant.book.txt80;

import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;

import app.netant.NetAntBase;

public class Txt80NetDetail extends NetAntBase {
	
	private Txt80Entity entity;

	public Txt80NetDetail(String netSite) {
		super(netSite);
	}
	
	public void setEntity(Txt80Entity entity){
		this.entity = entity;
	}
	
	public void analyseName(){
		
		Elements activePart = document.select("#soft_info_para h1");
		if(activePart.size()>0){
			String name = activePart.get(0).text();
			int index = name.indexOf("TXT");
			name = name.substring(0,index);
			
			this.entity.setNovelName(name);
			
		}
		
	}
	
	public void analyseTags(){
		Elements activePart = document.select("#soft_info_para .soft_info_r li");
		if(activePart.size()>0){
			for(int i=0;i<activePart.size()-2;i++){
				
				String txt = activePart.get(i).text();
				
				int point = txt.indexOf("：");
				String tagName = "";
				String tagValue = "";
				if(point > 0){
					tagName = txt.substring(0,point).trim();
				}
				if(point < txt.length()-1){
					tagValue = txt.substring(point+1).trim();
				}
				
				this.entity.setTag(tagName, tagValue);
				
			}
		}
	}
	
	public void analyseMemo(){
		Elements activePart = document.select("#mainSoftIntro");
		if(activePart.size()>0){
			String txt = activePart.get(0).text();		
			this.entity.setNovelMemo(txt);
		}
	}
	
	public void analyseHref(){
		Elements activePart = document.select("#soft_info_para .soft_info_r li");
		if(activePart.size()>0){
			Element child = activePart.get(activePart.size()-1);
			Elements aTags = child.select("a");
			if(aTags.size()>0){
				String href = aTags.get(0).attr("href");
				if(href.startsWith("/txtxz")){
					href = "https://www.80txt.com" + href;
				}
				
				Txt80NetDetail detail = null;
				try {
					
					System.out.println("下载地址访问:"+href);
					detail = new Txt80NetDetail(href);
					
					String newPath = detail.analyseUrl();					
					String fileFullName = newPath.substring(newPath.lastIndexOf("/")+1);
					String fileName = fileFullName.substring(0,fileFullName.lastIndexOf("."));
					
					System.out.println("解析地址为："+newPath);
					
					this.entity.setDownPath(newPath);
					this.entity.setNovelName(fileName);
					this.entity.setNovelFullName(fileFullName);
					
				} catch (Exception e) {
					e.printStackTrace();
				} finally {
					if(detail != null){
						detail.free();
					}
				}
				
			}
		}
	}
	
	public String analyseUrl(){
		String href = "";
		Elements activePart = document.select(".new_txt_down .txt_down_mide .down_url_txt .wanpan_url");
		if(activePart.size()>1){
			Elements aTags = activePart.get(1).select(".pan_url a");
			if(aTags.size()>0){
				href = aTags.get(0).attr("href");
			}
		}
		
		return href;
	}
	
	/**
	 * 信息分析
	 */
	public void analyse(){
		
		this.analyseName();
		this.analyseTags();		
		this.analyseHref();	
		this.analyseMemo();
		
		this.entity.download();
		this.entity.insert();
	}
	
	public static void main(String[] args) {
		
		Txt80NetDetail detail = new Txt80NetDetail("https://www.80txt.com/txtxz/1001.html");
		
		detail.analyseTags();
		
		detail.analyseMemo();
		
		detail.analyseHref();
		
	}

}
